{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/user/miniconda3/envs/yjq-ml/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "from model import OmniAnomaly\n",
    "import numpy as np\n",
    "import sys\n",
    "sys.path.append('../../common/')\n",
    "from evaluator import *\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_datase(dataset):\n",
    "    folder = os.path.join(\"../../processed\", dataset)\n",
    "    if not os.path.exists(folder):\n",
    "        raise Exception(\"Processed Data not found.\")\n",
    "    loader = []\n",
    "    for file in [\"train\", \"test\", \"labels\"]:\n",
    "        loader.append(np.load(os.path.join(folder, f\"{file}.npy\")))\n",
    "    ## 准备数据\n",
    "    train_data = loader[0]\n",
    "    test_data = loader[1]\n",
    "    labels = loader[2][:,0].reshape(-1,1)\n",
    "    return train_data, test_data, labels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SWaT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data, test_data, labels = load_datase(\"SWaT\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(495000, 51)\n",
      "(449919, 51)\n",
      "(449919, 1)\n"
     ]
    }
   ],
   "source": [
    "print(train_data.shape)\n",
    "print(test_data.shape)\n",
    "print(labels.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "self._valid_step_freq:100\n",
      "AE_layer 51 40 16 16\n",
      "AE_layer 40 51 16 16\n"
     ]
    }
   ],
   "source": [
    "model = OmniAnomaly(x_dims=train_data.shape[1], z_dims=40, window_size=12,max_epochs=5, valid_step_frep=100, batch_size=5120)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(346500, 51)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch:2/5 step:100 valid_loss:754.1925325393677 train_loss:23.452680587768555\n",
      "epoch:3/5 step:200 valid_loss:-659.8423271179199 train_loss:-28.20720863342285\n",
      "epoch:5/5 step:300 valid_loss:-1198.7915153503418 train_loss:-48.28828430175781\n"
     ]
    }
   ],
   "source": [
    "model.fit([train_data])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "score :(449908, 51) recon_mean:(449908, 51) z:(449908, 40)\n"
     ]
    }
   ],
   "source": [
    "score,_,_,_= model.predict(test_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "anomaly_score = -np.mean(score, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'f1-score': 0.7409020663210841,\n",
       " 'precision': 0.9950916833379355,\n",
       " 'recall': 0.5901576315720771,\n",
       " 'TP': 32235.0,\n",
       " 'TN': 395128.0,\n",
       " 'FP': 159.0,\n",
       " 'FN': 22386.0,\n",
       " 'threshold': 34113.42331222382}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bf_search(score=anomaly_score.squeeze(), label=labels[:-11].squeeze(), verbose=False, is_adjust=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'f1-score': 0.8413764454669292,\n",
       " 'precision': 0.886239312596491,\n",
       " 'recall': 0.8008458283808707,\n",
       " 'TP': 43743.0,\n",
       " 'TN': 389672.0,\n",
       " 'FP': 5615.0,\n",
       " 'FN': 10878.0,\n",
       " 'threshold': 1099.7055312499915}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bf_search(score=anomaly_score.squeeze(), label=labels[:-11].squeeze(), verbose=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## WADI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data, test_data, labels = load_datase(\"WADI\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(172801, 1)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "labels.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(172801, 123)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "self._valid_step_freq:200\n",
      "AE_layer 123 90 16 16\n",
      "AE_layer 90 123 16 16\n"
     ]
    }
   ],
   "source": [
    "model = OmniAnomaly(x_dims=train_data.shape[1], z_dims=90,window_size=10,max_epochs=5, valid_step_frep=200, batch_size=3072)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.fit([train_data])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "score :(172792, 123) recon_mean:(172792, 123) z:(172792, 90)\n"
     ]
    }
   ],
   "source": [
    "score,_,_,_= model.predict(test_data)\n",
    "anomaly_score = -np.mean(score, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'f1-score': 0.2612328630391476,\n",
       " 'precision': 0.9588424375637142,\n",
       " 'recall': 0.15121703838618963,\n",
       " 'TP': 1491.0,\n",
       " 'TN': 162868.0,\n",
       " 'FP': 64.0,\n",
       " 'FN': 8369.0,\n",
       " 'threshold': 16260324.761999998}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bf_search(score=anomaly_score.squeeze(), label=labels[:-9].squeeze(), verbose=False, is_adjust=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'f1-score': 0.40010384298557944,\n",
       " 'precision': 0.6013018702170426,\n",
       " 'recall': 0.2997971599393538,\n",
       " 'TP': 2956.0,\n",
       " 'TN': 160972.0,\n",
       " 'FP': 1960.0,\n",
       " 'FN': 6904.0,\n",
       " 'threshold': 16257829.719999997}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bf_search(score=anomaly_score.squeeze(), label=labels[:-9].squeeze(), verbose=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yjq-ml",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
